package dao;

import utils.DBHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ShopDao {

    // search框查询
    public List<Map<String, Object>> search(String text){
        // text 为要查询的内容
        String sql = "SELECT goodsname,image FROM shop WHERE 1=1 and goodsname like ? ";
        String params = "%" + text + "%";
        System.out.println(params);
        List<Map<String, Object>> data = DBHelper.selectListMap(sql ,params);
        return data;
    }

    // 组合条件查询
    public Map<String, Object> searchquery(String page,String company,String type,String order){
        String sql ="SELECT goodsname,image FROM shop WHERE 1=1 ";//升序asc 降序desc
        System.out.println(page+company+type+order);
        ArrayList<Object> params = new ArrayList<>();
        if (company != null && company.trim().isEmpty() == false){
            sql += " and company = ?";
            params.add(company);
        }
        if (type != null && type.trim().isEmpty() == false){
            sql += " and type = ?";
            params.add(type);
        }
        if (order != null && order.trim().isEmpty() == false){
            if ("asc".equals(order)){
                sql += " ORDER BY price ASC";
            }else if ("desc".equals(order)){
                sql += " ORDER BY price DESC";
            }
        }
        int pageSize = Integer.valueOf(page);
        pageSize = (pageSize - 1) * 8;// 计算显示的行数
        System.out.println(pageSize);
        System.out.println(params);
        long stotal = DBHelper.count(sql,params.toArray());
        sql +=" limit ?, 8 ";
        params.add(pageSize);
        List<Map<String, Object>> sdata = DBHelper.selectListMap(sql ,params.toArray());
        Map<String, Object> data = new HashMap<>();
        data.put("stotal", stotal);
        data.put("sdata", sdata);
        return data;
    }

    // 商品详情dialog查询
    public Map<String, Object> dialogquery(String goodsname){
        String sql ="SELECT s.company,s.goodsname,s.price,d.image,d.t1,d.t2,d.t3 FROM shop_dialog d, shop s WHERE d.goodsname = ? and d.goodsname = s.goodsname";
        System.out.println(goodsname);
        Map<String, Object> ddata = DBHelper.selectOneMap(sql ,goodsname);
        return ddata;
    }

    public List<Map<String, Object>> indexquery(){
        String sql ="SELECT goodsname,goodsinfo,image,price FROM shop";
        List<Map<String, Object>> data = DBHelper.selectListMap(sql);
        return data;
    }

    // 查询手机卡片的信息
    public Map<String, Object> phonequery(String page){
        int pageSize = Integer.valueOf(page);
        pageSize = (pageSize - 1) * 4;// 计算显示的行数
        String sql ="SELECT goodsname,goodsinfo,image,price FROM shop WHERE type='phone'";
        List<Map<String, Object>> phonedata = DBHelper.selectListMap(sql + " limit ?, 4 ",pageSize);
        long phonetotal = DBHelper.count(sql);// 查询到手机数据的总行数
        Map<String, Object> data = new HashMap<>();
        data.put("phonetotal", phonetotal);// 将手机总行数传进 Map集合
        data.put("phonedata", phonedata);// 将手机栏当前页的数据 传进 Map集合
        return data;
    }
    public Map<String, Object> comquery(String page){
        int pageSize = Integer.valueOf(page);
        pageSize = (pageSize - 1) * 4;// 计算显示的行数
        String sql ="SELECT goodsname,goodsinfo,image,price FROM shop WHERE type='computer'";
        List<Map<String, Object>> comdata = DBHelper.selectListMap(sql + " limit ?, 4 ",pageSize);
        long comtotal = DBHelper.count(sql);// 查询到电脑数据的总行数
        Map<String, Object> data = new HashMap<>();
        data.put("comtotal", comtotal);// 将电脑总行数传进 Map集合
        data.put("comdata", comdata);// 将电脑栏当前页的数据 传进 Map集合
        return data;
    }
    public Map<String, Object> earquery(String page){
        int pageSize = Integer.valueOf(page);
        pageSize = (pageSize - 1) * 4;// 计算显示的行数
        String sql ="SELECT goodsname,goodsinfo,image,price FROM shop WHERE type='earpod'";
        List<Map<String, Object>> eardata = DBHelper.selectListMap(sql + " limit ?, 4 ",pageSize);
        long eartotal = DBHelper.count(sql);// 查询到耳机数据的总行数
        Map<String, Object> data = new HashMap<>();
        data.put("eartotal", eartotal);// 将耳机总行数传进 Map集合
        data.put("eardata", eardata);// 将耳机栏当前页的数据 传进 Map集合
        return data;
    }
    public Map<String, Object> otherquery(String page){
        int pageSize = Integer.valueOf(page);
        pageSize = (pageSize - 1) * 4;// 计算显示的行数
        String sql ="SELECT goodsname,goodsinfo,image,price FROM shop WHERE type='other'";
        List<Map<String, Object>> otherdata = DBHelper.selectListMap(sql + " limit ?, 4 ",pageSize);
        long othertotal = DBHelper.count(sql);// 查询到手机数据的总行数
        Map<String, Object> data = new HashMap<>();
        data.put("othertotal", othertotal);// 将手机总行数传进 Map集合
        data.put("otherdata", otherdata);// 将手机栏当前页的数据 传进 Map集合
        return data;
    }
//    public Map<String, Object> cardquery(String page,String type){
//        int pageSize = Integer.valueOf(page);
//        pageSize = (pageSize - 1) * 4;// 计算显示的行数
//        String sql ="SELECT goodsname,goodsinfo,image,price FROM shop WHERE type=?";
//        Map<String, Object> data = new HashMap<>();
//        if ("phone".equals(type)){
//            long phonetotal = DBHelper.count(sql,type);// 查询到手机数据的总行数
//            List<Map<String, Object>> phonedata = DBHelper.selectListMap(sql + " limit ?, 4 ",type,pageSize);// 仅查询4行用于显示当前页
//            data.put("phonetotal", phonetotal);// 将手机总行数传进 Map集合
//            data.put("phonedata", phonedata);// 将手机栏当前页的数据 传进 Map集合
//        }else if ("computer".equals(type)){
//            long comtotal = DBHelper.count(sql,type);// 查询到手机数据的总行数
//            List<Map<String, Object>> comdata = DBHelper.selectListMap(sql + " limit ?, 4 ",type,pageSize);// 仅查询4行用于显示当前页
//            data.put("comtotal", comtotal);// 将电脑的总行数传进 Map集合
//            data.put("comdata", comdata);// 将电脑栏当前页的数据 传进 Map集合
//        }
//        return data;
//    }
}
